<php>$rand_key = time() . mt_rand(10000,99999);</php>
<input type="file" id="upload_picture_{$rand_key}" class="hide">
<input name="item_id" id="item_id_{$rand_key}" type="hidden" value="{$param.item_id}">
<div class="upload-img-box dragsort J-uib-{$rand_key}">
  <div style="display: none;"></div><!-- 新增一个隐藏层，解决无图片的时候初始化图片拖拽排序插件失败的问题 -->
  <input type="hidden" name="{$param.name}" value="{$param['images'][0][id]}" class="icon" />
  <notempty name="param.images">
    <volist name="param.images" id="vo">
      <div class="upload-pre-item J_item_{$rand_key}" id="item_{$vo['id']}_{$rand_key}">
        <img src="__ROOT__{$vo['path']}" data-id="{$vo['id']}" title="点击显示大图" />
        <span class='btn-close icon delPic' title='删除图片' data-rand='{$rand_key}'></span>
        <label class="set" for="default_{$vo['id']}"><input type="radio" name="thumb" class="thumb" value="{$vo['id']}" id="default_{$vo['id']}" <notempty name="param.thumb"><eq name="vo.id" value="$param.thumb">checked="checked"</eq><else/><eq name="key" value="0">checked="checked"</eq></notempty>> 设为封面</label>
      </div>
    </volist>
  </notempty>
</div>
<script type="text/javascript">
  //限制传单张
  var uploadify_{$rand_key} = function(){
    /* 初始化上传插件 */
    $("#upload_picture_{$rand_key}").uploadify({
      "height": 36,
      "swf": "__JS__/uploadify/uploadify.swf?ver=" + Math.random(),
      "fileObjName": "download",
      "buttonText": "上传图片",
      "uploader": "{:U('File/uploadPicture',array('session_id'=>session_id()))}",
      "width": 80,
      'removeTimeout': 1,
      'fileTypeExts': '*.jpg; *.png; *.gif;',
      "onUploadSuccess": uploadPicture{$rand_key},
      "simUploadLimit": 1,
      "multi": false,
      'onFallback': function(){
        alert('未检测到兼容版本的Flash');
      }
    });
    if($(".J_item_{$rand_key}").size() > 0){
      $("#upload_picture_{$rand_key}").hide();
    }
  }
  
  //
  if(typeof(swf_upload_i) === 'undefined'){ swf_upload_i = 0;}
  if(typeof SWFUpload === 'undefined' && swf_upload_i == 0){
    swf_upload_i = 1;
    $.getScript("__JS__/uploadify/jquery.uploadify.min.js");
  }
  var upload_init_{$rand_key} = function(){
    if(typeof SWFUpload !== 'undefined'){
      window.clearInterval(upload_timer{$rand_key});
      uploadify_{$rand_key}();
    }
  }
  upload_timer{$rand_key} = window.setInterval(upload_init_{$rand_key},1e2);
  upload_init_{$rand_key}();
  
  function uploadPicture{$rand_key}(file, data){
    var data = eval("(" + data + ")");
    //var data = $.parseJSON(data);
    var src = '';
    if(data.status){
      //创建<div class="upload-pre-item">
      var upload_item = $("<div class='upload-pre-item' id='item_" + data.id + "_{$rand_key}'></div>");
      //创建img input close-btn
      $(".J-uib-{$rand_key} input[name='{$param.name}']").val(data.id);
      src = data.url || '__ROOT__' + data.path;
      var upload_img = $("<img src=" + src + " title='点击显示大图，拖动图片可以对图片进行排序。' data-id=" + data.id + ">");
      var img_del = $("<span class='btn-close delPic' title='删除图片' data-rand='{$rand_key}'></span>");
      var set_thumb = $('<label class="set" for="default_' + data.id + '"><input type="radio" name="thumb" class="thumb" value="' + data.id + '" id="default_' + data.id + '"> 设为封面</label>');

      //加入到upload-img-box
      upload_item.append(upload_img);
      upload_item.append(img_del);
      upload_item.append(set_thumb);
      $('.J-uib-{$rand_key}').append(upload_item);

  
      // 设置默认封面图片
      var obj_thumb = $('input.thumb:checked');
      if(obj_thumb.length == 0){
        $(".upload-img-box > div:eq(1) > label > input.thumb").attr("checked", true)
      }
      $("#upload_picture_{$rand_key}").hide();
      $('.uploadify-queue-item').slideUp();
    }else{
      updateAlert(data.info);
      setTimeout(function(){
        $('#top-alert').find('button').click();
        $(that).removeClass('disabled').prop('disabled', false);
      }, 1500);
    }
    
  }
  
  // 删除图片
  $(document).on("click",".delPic",function(){
    $("#upload_picture_"+ $(this).data('rand')).show();
    $(".J-uib-"+ $(this).data('rand') +" input[name='{$param.name}']").val('');
    $(this).parent().remove();
  });
</script>